<?php

namespace app\index\controller;

use think\console\output\descriptor\Console;
use think\Controller;
use think\Request;
use app\index\controller\Common;
use app\index\model\Messages as MessagesModel;

class Messages extends Common
{
    public function add(){
        // return \json(['code' => 1 , 'msg' => '留言成功']);
        // die;
        if (!session('userid') || !session('username')) {
            return \json(['code' => 2 , 'msg' => '您尚未登录!']);
        }
        $mess = input('post.');
        $time = time();
        $ctime = date("Y-m-d H:i:s",$time);
        $data = [
            'content' => $mess['content'],
            'user_id' => $mess['id'],
            'article_id' => $mess['artresid'],
            'create_time' => $ctime
        ];
        $res = db('messages')->insert($data);
        if($res){
            return \json(['code' => 1 , 'msg' => '留言成功']);
        }else{
            return \json(['code' => 0 , 'msg' => '留言失败']);
        }
    }

    public function mess_user(){
        $mess = new MessagesModel();
        $res = $mess->get_mess_user();
        $this->assign('mess_user', $res);
        return view('messages/messages');
    }

    public function del(){
        $id = input('id');
        $res = db('messages')->delete(['id' => $id]);
        if($res){
            return \json(['code' => 1 , 'msg' => '删除留言成功']);
        }else{
            return \json(['code' => 0 , 'msg' => '删除留言失败']);
        }
    }
    
}
